<!--
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-07-30 20:02:30
 * @LastEditTime: 2020-02-28 11:25:38
 * @LastEditors: Please set LastEditors
 -->
<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>Hello APP</title>
    <link rel="stylesheet" type="text/css" href="../css/vant.css">
    <link rel="stylesheet" type="text/css" href="../css/base.css" />
    <script type="text/javascript" src="../script/jquery.js"></script>
    <script type="text/javascript" src="../script/layer/mobile/layer.js"></script>
    <script type="text/javascript" src="../script/vue.min.js"></script>
    <script type="text/javascript" src="../script/vant.min.js"></script>
    <script type="text/javascript" src="../script/fastclick.js"></script>
    <script type="text/javascript" src="../script/base.js"></script>
    <style type="text/css">
    </style>
</head>

<body>
    <div id="app" v-cloak>
    </div>
</body>

<script type="text/javascript">
    var vm;
    apiready = function () {

        var demo = api.require('xGprinterTsc');

        api.addEventListener({
            name: 'printStatus'
        }, function (ret) {
            if (ret.value.code) {
                // alert('单张' + JSON.stringify(ret.value.code))
                vm.checkPrintStatus(ret.value.code)
            } else if (ret.value.totalCode) {
                // alert('批量' + JSON.stringify(ret.value.totalCode))
                ret.value.totalCode.map((item) => {
                    item.ProductCode = '产品编码：' + item.ProductCODE
                    item.ProductionNO = '生产订单：' + item.ProductionNO
                    item.date = '生产日期：' + item.ReporTime.split('T')[0]
                    item.time = item.ReporTime.split('T')[1]
                    item.BaoZhangQTY = '本箱数量：' + item.ReporQTY
                    item.beforeEquipment ? item.EquipmentID = '设备编号：' + item.beforeEquipment : item.EquipmentID = '设备编号：' + item.EquipmentCode
                    item.empolorName = '操作员：' + item.empolorName
                    item.frontProductName = '产品名称：' + item.ProductName.slice(0, 10)
                    item.afterProductName = item.ProductName.slice(10, item.ProductName.length)
                    item.beforeBatch ? item.batch = '产品批次：' + item.beforeBatch + item.Batch : item.batch = '产品批次：' + item.Batch
                    item.boxCode = item.FBoxCode
                    item.lastBoxCode = '本箱序号：' + item.FBoxNum
                    item.GX = '工序：' + item.GX

                    vm.checkPrintStatus(item)
                })
            }
        })

        api.addEventListener({
            name: 'loughtOut'
        }, function (ret, err) {
            api.confirm({
                msg: '是否确认退出？',
                buttons: ['确认退出', '取消']
            }, function (confirmret, confirmerr) {
                var index = confirmret.buttonIndex;
                if (index == 1) {
                    APP.ajax('ZS_DownEquipment', { EquipmentID: ret.value.equipment.EquipmentID }, function (res) {
                        // console.log('res', JSON.stringify(res))
                        if (res.ds[0].status == 200) {
                            vm.$toast(res.ds[0].msg)
                            api.openFrame({
                                name: 'login',
                                url: 'login.html'
                            })
                        } else {
                            vm.$toast('下机失败请重试')
                        }
                    })
                }
            });
        });

        api.addEventListener({
            name: 'setPrint'
        }, function (ret) {
            vm.setPrint()
        })

        vm = new Vue({
            el: '#app',
            data: {
                currentNetStatus: 1,
                deviceName: '',
                totalCode: []
            },
            created: function () {
                var that = this;

                window.localStorage.setItem('clientHeight', api.winHeight)
                // alert('clientHeight' + JSON.stringify(api.winHeight))
                if (api.winHeight != 752 && api.winHeight != 720 && api.winHeight != 512) { //普通设备
                    // alert('没进入打印')
                    // console.log('11')
                } else { //工控机
                    // that.checkNet()
                    that.setPrint()

                    // alert('进入打印了')
                    setInterval(() => {
                        var Equipment = JSON.parse(window.localStorage.getItem('Equipment'))
                        // alert('Equipment' + JSON.stringify(Equipment))
                        if (Equipment && Equipment.EquipmentName.indexOf('涂油设备') != -1) {
                            APP.ajax('TYDuoDa', { EquipmentID: Equipment.EquipmentID }, function (res) {
                                // alert('res' + JSON.stringify(res))
                                if (res) {
                                    res.ds.map((item) => {
                                        that.printTYLabel(item.XMCode, item.productname, '时间：' + item.DayTime, '箱序：' + item.FBoxNum, '数量:' + item.nums)
                                    })
                                }
                            })
                        }
                    }, 5000)
                }
                var Equipment = JSON.parse(window.localStorage.getItem('Equipment'))
                // console.log('Equipment' + JSON.stringify(Equipment))
                api.openFrame({
                    name: 'login',
                    url: 'login.html',
                })
            },
            methods: {
                checkNet() {
                    var that = this
                    var url = 'http://' + APP.serverIP() + '/Frame/ST1_LLS/Handler.aspx?cmd=test';
                    setInterval(() => {
                        $.ajax({
                            url: url,
                            timeout: 300,
                            data: {},
                            type: "post",
                            success: function (ret) {
                                if (ret) {
                                    api.sendEvent({
                                        name: 'checkNetStatus',
                                        extra: {
                                            res: '网络正常'
                                        }
                                    })
                                }
                            },
                            error: function (ret) {
                                if (ret) {
                                    api.sendEvent({
                                        name: 'checkNetStatus',
                                        extra: {
                                            err: '网络异常'
                                        }
                                    })
                                }
                            }
                        });
                    }, 1000)
                },
                setPrint() {
                    var that = this
                    demo.getUsbDeviceList(function (ret, err) {
                        // alert(JSON.stringify(ret));
                        if (!err) {
                            if (ret.data.length) {
                                that.deviceName = ret.data[0].deviceName
                                that.connectUSB()
                            } else {
                                api.confirm({
                                    title: '提示',
                                    msg: '是否确认取消连接打印机',
                                    buttons: ['确定', '取消']
                                }, function (ret, err) {
                                    var index = ret.buttonIndex;
                                    if (ret.buttonIndex != 1) {
                                        that.setPrint()
                                    }
                                });
                                // api.alert({
                                //     title: '提示',
                                //     msg: '打印机连接错误:' + JSON.stringify(err),
                                // }, function (alertRet, alerterr) {
                                //     if (alertRet) {
                                //         that.setPrint()
                                //     }
                                // });
                            }
                        } else {
                            api.confirm({
                                title: '提示',
                                msg: '是否确认取消连接打印机',
                                buttons: ['确定', '取消']
                            }, function (ret, err) {
                                var index = ret.buttonIndex;
                                if (ret.buttonIndex != 1) {
                                    that.setPrint()
                                }
                            });
                            // api.alert({
                            //     title: '获取打印机列表报错!',
                            //     msg: JSON.stringify(err),
                            // }, function (alertRet, alerterr) {
                            //     if (alertRet) {
                            //         that.setPrint()
                            //     }
                            // });
                        }
                    })
                },
                connectUSB() {
                    var that = this
                    demo.connectUsb({
                        deviceName: that.deviceName
                    }, function (ret, err) {
                        // alert(JSON.stringify(ret));
                        if (!err) {
                            if (ret.evenType == 'onSuccess') {
                                api.toast({
                                    msg: that.deviceName + '打印机连接成功!',
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        } else {
                            api.alert({
                                title: '打印机USB连接报错!',
                                msg: JSON.stringify(err),
                            }, function (alertRet, alerterr) {
                                if (alertRet) {
                                    that.setPrint()
                                }
                            });
                        }
                    })
                },

                checkPrintStatus(code) {
                    var that = this
                    var ret = demo.getConnectState();
                    // alert(JSON.stringify(ret));
                    if (ret) {
                        api.toast({
                            msg: that.deviceName + '打印机连接正常!',
                            duration: 2000,
                            location: 'bottom'
                        });
                        demo.checkState(function (stateRet, stateErr) {
                            // alert(JSON.stringify(ret))
                            if (ret) {
                                api.toast({
                                    msg: that.deviceName + '打印机状态正常!',
                                    duration: 2000,
                                    location: 'bottom'
                                });
                                that.printLabel(code)
                            } else {
                                api.alert({
                                    title: '提示',
                                    msg: '打印机状态异常,请检查!' + JSON.stringify(stateErr),
                                }, function (alertRet, alerterr) {
                                    if (alertRet) {
                                        that.setPrint()
                                    }
                                });
                            }
                        });
                    } else {
                        api.alert({
                            title: '提示',
                            msg: '打印机连接异常,请检查!',
                        }, function (alertRet, alerterr) {
                            if (alertRet) {
                                that.setPrint()
                            }
                        });
                    }
                },
                printLabel(print) {
                    try {
                        var that = this
                        // alert(JSON.stringify('开始打印了'))
                        var month = '(' + (new Date().getMonth() + 1) + ')月份标签'
                        demo.printTsc({
                            rows: [
                                { lineType: 'size', width: 100, height: 80.4 },
                                { lineType: 'gap', m: 2, n: 0 },
                                { lineType: 'reference', x: 0, y: 0 },
                                { lineType: 'tear', data: 'true' },
                                { lineType: 'addQueryPrinterStatus', status: 'ON' },
                                { lineType: 'cls' },
                                {
                                    lineType: 'addPeel',//设置打印机剥离模式
                                    tear: 'ON'// ON/OFF 是否开启
                                },
                                {
                                    lineType: 'text', x: 250, y: 48, font: 13, rotation: 0, xScal: 1,
                                    yScal: 1, data: '宁波劳伦斯表面技术有限公司'
                                },
                                {//绘制文本
                                    lineType: 'text',
                                    x: 310,
                                    y: 88,
                                    font: 13,
                                    rotation: 0,
                                    xscal: 1,
                                    yscal: 1,
                                    data: month
                                },
                                {//绘制文本
                                    lineType: 'text',
                                    x: 60,
                                    y: 110,
                                    font: 13,
                                    rotation: 0,
                                    xscal: 1,
                                    yscal: 1,
                                    data: print.ProductCode
                                },
                                {//绘制文本
                                    lineType: 'text',
                                    x: 60,
                                    y: 170,
                                    font: 13,
                                    rotation: 0,
                                    xscal: 1,
                                    yscal: 1,
                                    data: print.ProductionNO
                                },
                                {//绘制文本
                                    lineType: 'text',
                                    x: 60,
                                    y: 240,
                                    font: 13,
                                    rotation: 0,
                                    xscal: 1,
                                    yscal: 1,
                                    data: print.date
                                },
                                {//绘制文本
                                    lineType: 'text',
                                    x: 180,
                                    y: 300,
                                    font: 13,
                                    rotation: 0,
                                    xscal: 1,
                                    yscal: 1,
                                    data: print.time
                                },
                                {//绘制文本
                                    lineType: 'text',
                                    x: 60,
                                    y: 360,
                                    font: 13,
                                    rotation: 0,
                                    xscal: 1,
                                    yscal: 1,
                                    data: print.BaoZhangQTY
                                },
                                {//绘制文本
                                    lineType: 'text',
                                    x: 60,
                                    y: 420,
                                    font: 13,
                                    rotation: 0,
                                    xscal: 1,
                                    yscal: 1,
                                    data: print.lastBoxCode
                                },
                                {//绘制文本
                                    lineType: 'text',
                                    x: 60,
                                    y: 480,
                                    font: 13,
                                    rotation: 0,
                                    xscal: 1,
                                    yscal: 1,
                                    data: print.EquipmentID
                                },
                                {//绘制文本
                                    lineType: 'text',
                                    x: 60,
                                    y: 540,
                                    font: 13,
                                    rotation: 0,
                                    xscal: 1,
                                    yscal: 1,
                                    data: print.empolorName
                                },
                                {//绘制文本
                                    lineType: 'text',
                                    x: 390,
                                    y: 120,
                                    font: 13,
                                    rotation: 0,
                                    xscal: 1,
                                    yscal: 1,
                                    data: print.frontProductName
                                },
                                {//绘制文本
                                    lineType: 'text',
                                    x: 390,
                                    y: 160,
                                    font: 13,
                                    rotation: 0,
                                    xscal: 1,
                                    yscal: 1,
                                    data: print.afterProductName
                                },
                                {//绘制文本
                                    lineType: 'text',
                                    x: 390,
                                    y: 220,
                                    font: 13,
                                    rotation: 0,
                                    xscal: 1,
                                    yscal: 1,
                                    data: print.batch
                                },
                                {
                                    lineType: 'qrcode', //绘制qrcode,
                                    x: 390,
                                    y: 280,
                                    level: 'L',  //L|M|H|Q
                                    cellwith: 9,
                                    rotation: 0, // 0|90|180|270
                                    data: print.boxCode
                                },
                                {//绘制文本
                                    lineType: 'text',
                                    x: 390,
                                    y: 520,
                                    font: 13,
                                    rotation: 0,
                                    xscal: 1,
                                    yscal: 1,
                                    data: print.boxCode
                                },
                                {//绘制文本
                                    lineType: 'text',
                                    x: 390,
                                    y: 560,
                                    font: 13,
                                    rotation: 0,
                                    xscal: 1,
                                    yscal: 1,
                                    data: print.GX
                                },
                                { lineType: 'addPrint', m: 1, n: 1 },
                                { lineType: 'queryPrinterStatus' },
                            ],
                            isQueryState: true,
                        }, function (ret, err) {
                            if (ret) {
                                // alert('ret' + JSON.stringify(ret))
                                api.sendEvent({
                                    name: 'refreshProductReport',
                                    extra: {
                                        ret: ret
                                    }
                                })
                            } else {
                                // alert('err' + JSON.stringify(err))
                                api.sendEvent({
                                    name: 'refreshProductReport',
                                    extra: {
                                        err: err
                                    }
                                })
                            }
                        });
                    } catch (e) {
                        api.alert({
                            title: '提示',
                            msg: '打印方法报错' + e,
                        }, function (alertRet, alerterr) {
                        });
                    }
                },
                printTYLabel(code, productname, DayTime, FBoxNum, nums) {
                    try {
                        var that = this
                        var month = '(' + (new Date().getMonth() + 1) + ')月份标签'
                        demo.printTsc({
                            rows: [
                                { lineType: 'size', width: 100, height: 80.4 },
                                { lineType: 'gap', m: 2, n: 0 },
                                { lineType: 'reference', x: 0, y: 0 },
                                { lineType: 'tear', data: 'true' },
                                { lineType: 'addQueryPrinterStatus', status: 'ON' },
                                { lineType: 'cls' },
                                {
                                    lineType: 'addPeel',//设置打印机剥离模式
                                    tear: 'ON'// ON/OFF 是否开启
                                },
                                {
                                    lineType: 'qrcode', //绘制qrcode,
                                    x: 250,
                                    y: 50,
                                    level: 'L',  //L|M|H|Q
                                    cellwith: 12,
                                    rotation: 0, // 0|90|180|270
                                    data: code
                                },
                                {//绘制文本
                                    lineType: 'text',
                                    x: 240,
                                    y: 340,
                                    font: 13,
                                    rotation: 0,
                                    xscal: 1,
                                    yscal: 1,
                                    data: code
                                },
                                {//绘制文本
                                    lineType: 'text',
                                    x: 240,
                                    y: 390,
                                    font: 13,
                                    rotation: 0,
                                    xscal: 1,
                                    yscal: 1,
                                    data: productname
                                },
                                {//绘制文本
                                    lineType: 'text',
                                    x: 240,
                                    y: 440,
                                    font: 13,
                                    rotation: 0,
                                    xscal: 1,
                                    yscal: 1,
                                    data: DayTime
                                },
                                {//绘制文本
                                    lineType: 'text',
                                    x: 240,
                                    y: 490,
                                    font: 13,
                                    rotation: 0,
                                    xscal: 1,
                                    yscal: 1,
                                    data: FBoxNum
                                },
                                {//绘制文本
                                    lineType: 'text',
                                    x: 240,
                                    y: 540,
                                    font: 13,
                                    rotation: 0,
                                    xscal: 1,
                                    yscal: 1,
                                    data: nums
                                },
                                {//绘制文本
                                    lineType: 'text',
                                    x: 240,
                                    y: 590,
                                    font: 13,
                                    rotation: 0,
                                    xscal: 1,
                                    yscal: 1,
                                    data: '工序：涂油'
                                },
                                { lineType: 'addPrint', m: 1, n: 1 },
                                { lineType: 'queryPrinterStatus' },
                            ],
                            isQueryState: true,
                        }, function (ret, err) {
                            if (ret) {
                                // alert('TYret' + JSON.stringify(ret))
                            } else {
                                // alert('TYerr' + JSON.stringify(err))
                            }
                        });
                    } catch (e) {
                        api.alert({
                            title: '提示',
                            msg: '打印方法报错' + e,
                        }, function (alertRet, alerterr) {
                        });
                    }
                }
            }
        })
    };
</script>

</html>